package com.anti.modular.bus.houselease.controller;

import cn.hutool.core.date.DateUtil;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.commom.api.Result;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.bus.busrealestate.result.excel.StateExcel;
import com.anti.modular.bus.houselease.param.HouseLeaseParam;
import com.anti.modular.bus.houselease.param.HouseLeaseParam2;
import com.anti.modular.bus.houselease.param.HouseLeaseTzParam;
import com.anti.modular.bus.houselease.result.excel.LeaseExcel;
import com.anti.modular.bus.houselease.service.HouseLeaseService;
import com.anti.modular.commonutils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import sun.util.resources.ga.LocaleNames_ga;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 房间租约控制器
 *
 * @author anti
 * @date 2021-05-06 09:30:21
 */
@RestController
@RequestMapping("houseLease")
public class HouseLeaseController {

    @Resource
    private HouseLeaseService houseLeaseService;

    /**
     * 查询房间租约
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @GetMapping("page")
    @BusinessLog(title = "房间租约_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result page(PageParam pageParam, HouseLeaseParam houseLeaseParam) {
        return Result.data(houseLeaseService.page(pageParam,houseLeaseParam));
    }

    /**
     * 添加房间租约
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("add")
    @BusinessLog(title = "房间租约_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(HouseLeaseParam.add.class) HouseLeaseParam houseLeaseParam) {
            houseLeaseService.add(houseLeaseParam);
        return Result.success("新增成功");
    }

    /**
     * 删除房间租约
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("delete")
    @BusinessLog(title = "房间租约_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(HouseLeaseParam.delete.class) HouseLeaseParam houseLeaseParam) {
            houseLeaseService.delete(houseLeaseParam);
        return Result.success("删除成功");
    }

    /**
     * 编辑房间租约
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("edit")
    @BusinessLog(title = "房间租约_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(HouseLeaseParam.edit.class) HouseLeaseParam houseLeaseParam) {
            houseLeaseService.edit(houseLeaseParam);
        return Result.success("修改成功");
    }
    /**
     * 编辑房间租客
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("editZk")
    @BusinessLog(title = "房间租约_租客编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editZk(@RequestBody @Validated(HouseLeaseParam.editZk.class) HouseLeaseParam houseLeaseParam) {
            houseLeaseService.editZk(houseLeaseParam);
        return Result.success("修改成功");
    }
    /**
     * 编辑房间费用水电周期
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("editFy")
    @BusinessLog(title = "房间租约_費用清單编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editFy(@RequestBody HouseLeaseParam2 houseLeaseParam) {
        houseLeaseService.editFy(houseLeaseParam);
        return Result.success("修改成功");
    }
    @PostMapping("editJs")
    @BusinessLog(title = "房间租约_家私编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editJs(@RequestBody HouseLeaseParam2 houseLeaseParam) {
        houseLeaseService.editJs(houseLeaseParam);
        return Result.success("修改成功");
    }

    /**
     * 编辑房间租约退租
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @PostMapping("editTuiZu")
    @BusinessLog(title = "房间租约_退租", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editTuiZu(@RequestBody @Validated(HouseLeaseTzParam.edit.class) HouseLeaseTzParam houseLeaseParam) {
        houseLeaseService.editTuiZu(houseLeaseParam);
        return Result.success("退租成功");
    }

    @PostMapping("editXuzu")
    @BusinessLog(title = "房间租约_续租", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editXuzu(@RequestBody @Validated(HouseLeaseTzParam.editXuZu.class) HouseLeaseTzParam houseLeaseParam) {
        houseLeaseService.editXuzu(houseLeaseParam);
        return Result.success("退租成功");
    }

    /**
     * 查看房间租约
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @GetMapping("detail")
    public Result detail(@Validated(HouseLeaseParam.detail.class) HouseLeaseParam houseLeaseParam) {
        return Result.data(houseLeaseService.detail(houseLeaseParam));
    }

    /**
     * 房间租约列表
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @GetMapping("list")
    public Result list(HouseLeaseParam houseLeaseParam) {
        return Result.data(houseLeaseService.list(houseLeaseParam));
    }
    /**
     * 查看在租房间租约具体信息
     *
     * @author anti
     * @date 2021-05-06 09:30:21
     */
    @GetMapping("getIsLeaseMsgForPrint/{id}")
    public Result getIsLeaseMsgForPrint(@PathVariable(name = "id") Long id) {
        return Result.data(houseLeaseService.getIsLeaseMsgForPrint(id));
    }

    @GetMapping("getTjSys")
    public Result getTjSys(HouseLeaseParam houseLeaseParam) {
        return Result.data(houseLeaseService.getTjSys(houseLeaseParam));
    }

    @PostMapping("exportExcel")
    @BusinessLog(title = "房產_導出EXCEL", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void exportExcel(@RequestBody HouseLeaseParam houseLeaseParam,HttpServletResponse response){
        List<LeaseExcel> exportData = houseLeaseService.getExportData(houseLeaseParam);
        String title="租约信息";
        String fileName="租约信息"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        try {
            ExcelUtils.exportExcel(exportData,title,title, LeaseExcel.class, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("getLeaseFurnitureListById/{id}")
    public Result getLeaseFurnitureList(@PathVariable(name = "id") Long id) {
        return Result.data(houseLeaseService.getLeaseFurnitureList(id));
    }

}
